//
//  RegistViewController.m
//  BasketballClub
//
//  Created by 孙鸿吉 on 16/1/14.
//  Copyright © 2016年 孙鸿吉. All rights reserved.
//

#import "RegistViewController.h"
#import "RegistView.h"
#import <AVOSCloud.h>
@interface RegistViewController ()<UITextFieldDelegate>
@property (nonatomic,strong)RegistView *registView;
@end

@implementation RegistViewController
- (void)loadView
{
    self.registView = [[RegistView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.registView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.registView.backgroundColor = ColorHex(0x0d1c38);
    //遵守协议
    self.registView.usernameTF.delegate = self;
    self.registView.passwordTF.delegate = self;
    self.registView.rePasswordTF.delegate = self;
    self.registView.emailTF.delegate = self;
    //返回按钮方法
    [self.registView.cancleButton addTarget:self action:@selector(returnBeforeAction) forControlEvents:UIControlEventTouchUpInside];
    //注册按钮方法
    [self.registView.registButton addTarget:self action:@selector(registNewUserAction) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - 键盘回收
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField.tag >= 1001 && textField.tag <= 1004) {
        UITextField *field = (UITextField *)[self.registView viewWithTag:textField.tag + 1];
        [field becomeFirstResponder];
    }
    [textField resignFirstResponder];
    return YES;
}
//点击屏幕空白处回收键盘
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.registView endEditing:YES];
}
#pragma mark - 左边返回按钮方法实现
- (void)returnBeforeAction
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 注册按钮方法实现
- (void)registNewUserAction
{
    //注册,第一次注册的新账号都会赋值一个key对应1的值 目的是为了标记是第一次登陆,为以后的创建字段做准备
    AVUser *user = [AVUser user];
    user.username = self.registView.usernameTF.text;
    user.password = self.registView.passwordTF.text;
    user.email = self.registView.emailTF.text;
    [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            NSString *str = [NSString stringWithFormat:@"%@isFirstLogin",user.username];
            [[LeanCloudService shareService] setIsFirstLoginWithNumber:1 andKye:str];
            [[LeanCloudService shareService] synchronize];
            [self dismissViewControllerAnimated:YES completion:nil];
        }else{
            
        }
    }];
}
#pragma mark - 判断键盘方法
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if ([textField isEqual:self.registView.usernameTF]) {
        
        if (self.registView.usernameTF.text.length < 8) {
            self.registView.errorButton1.hidden = NO;
            [self.registView.errorButton1 setImage:[UIImage imageNamed:@"iconfont-tanhao"] forState:UIControlStateNormal];
            [self.registView.errorButton1 addTarget:self action:@selector(infoOferror1) forControlEvents:UIControlEventTouchUpInside];
        }else{
            self.registView.errorButton1.hidden = NO;
            [self.registView.errorButton1 setImage:[UIImage imageNamed:@"iconfont-duihao"] forState:UIControlStateNormal];
        }
    }
    if ([textField isEqual:self.registView.passwordTF]) {
        if (self.registView.passwordTF.text.length < 6) {
            self.registView.errorButton2.hidden = NO;
            [self.registView.errorButton2 setImage:[UIImage imageNamed:@"iconfont-tanhao"] forState:UIControlStateNormal];
            [self.registView.errorButton2 addTarget:self action:@selector(infoOferror2) forControlEvents:UIControlEventTouchUpInside];
        }else{
            self.registView.errorButton2.hidden = NO;
            [self.registView.errorButton2 setImage:[UIImage imageNamed:@"iconfont-duihao"] forState:UIControlStateNormal];
        }
    }
    if ([textField isEqual:self.registView.rePasswordTF]) {
        if ([self.registView.rePasswordTF.text isEqualToString:self.registView.passwordTF.text]) {
            self.registView.errorButton3.hidden = NO;
            [self.registView.errorButton3 setImage:[UIImage imageNamed:@"iconfont-duihao"] forState:UIControlStateNormal];
            
        }else{
            [self.registView.errorButton3 addTarget:self action:@selector(infoOferror3) forControlEvents:UIControlEventTouchUpInside];
            [self.registView.errorButton3 setImage:[UIImage imageNamed:@"iconfont-tanhao"] forState:UIControlStateNormal];
            self.registView.errorButton3.hidden = NO;

        }
    }
}
#pragma mark - 弹窗提示信息方法
- (void)infoOferror1
{
    [UIView animateWithDuration:0.3 animations:^{
        self.registView.errorImageView1.frame = CGRectMake(SCREEN_WIDTH - 90 * LAYOUT_SIZE, 120 * LAYOUT_SIZE_HEIGHT, 80 * LAYOUT_SIZE, 30 * LAYOUT_SIZE_HEIGHT);
    }];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.registView.errorImageView1.frame = CGRectMake(SCREEN_WIDTH, 120 * LAYOUT_SIZE_HEIGHT, 80 * LAYOUT_SIZE, 30 * LAYOUT_SIZE_HEIGHT);
    });
    
}

- (void)infoOferror2
{
    [UIView animateWithDuration:0.3 animations:^{
        self.registView.errorImageView2.frame = CGRectMake(SCREEN_WIDTH - 90 * LAYOUT_SIZE, 175 * LAYOUT_SIZE_HEIGHT, 80 * LAYOUT_SIZE, 30 * LAYOUT_SIZE_HEIGHT);
    }];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.registView.errorImageView2.frame = CGRectMake(SCREEN_WIDTH, 175 * LAYOUT_SIZE_HEIGHT, 80 * LAYOUT_SIZE, 30 * LAYOUT_SIZE_HEIGHT);
    });
    
}

- (void)infoOferror3
{
    [UIView animateWithDuration:0.3 animations:^{
        self.registView.errorImageView3.frame = CGRectMake(SCREEN_WIDTH - 90 * LAYOUT_SIZE, 225 * LAYOUT_SIZE_HEIGHT, 80 * LAYOUT_SIZE, 30 * LAYOUT_SIZE_HEIGHT);
    }];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.registView.errorImageView3.frame = CGRectMake(SCREEN_WIDTH, 225 * LAYOUT_SIZE_HEIGHT, 80 * LAYOUT_SIZE, 30 * LAYOUT_SIZE_HEIGHT);
    });
    
}












- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
